<?php

class HotelroomsController extends CController
{
	public function actionIndex()
	{
		$hotelId = intval($_GET['id']);
		
		$this->setPageTitle('Отели');
		
		$columns = HotelDateGroup::model()->criteria()->with(array('hotelDates'))->findAll('hotelId=:hotelId', array(':hotelId' => $hotelId));
		
		$this->renderPartial('index', array(
			'hotelId' => $hotelId,
			'columns' => $columns 
		), false, true);
	}
	
	public function actionGetdata()
	{
		$hotelId = intval($_POST['hotelId']);
		$sort    = $_POST['sort'];
		$order   = $_POST['dir'];
		
		$this->renderPartial('json', array(
			'data'    => HotelRoomAndDates::getPricesWithDates($hotelId, ($sort == 'title' ? ($sort . ' ' . $order) : null)),
			'columns' => HotelDateGroup::model()->criteria()->findAll('hotelId=:hotelId', array(':hotelId' => $hotelId)),
			'sort'    => $sort,
			'order'   => $order
		), false, true);
	}
	
	public function actionAdd()
	{
		$hotelId = intval($_POST['hotelId']);
		
		if (strval($_POST['title']) != '') {
			
			$returnArr = array();
			
			$columns = HotelDateGroup::model()->findAll('hotelId=:hotelId', array(':hotelId' => $hotelId));
			
			$hotelRoom = new HotelRoom();
			$hotelRoom->hotelId = $hotelId;
			$hotelRoom->title   = $_POST['title'];
			
			if ($hotelRoom->save()) {
				
				$returnArr['id'] = $hotelRoom->id;
				$returnArr['title']  = $_POST['title'];
				$returnArr['action'] = 'icon-delete-item';
				
				foreach ($columns as $col) {
					
					$key = "dateField{$col->id}";
					
					if (array_key_exists($key, $_POST)) {
						$hotelRoomAndDates = new HotelRoomAndDates();
						$hotelRoomAndDates->hotelRoomId = $hotelRoom->id;
						$hotelRoomAndDates->hotelDateGroupId = $col->id;
						$hotelRoomAndDates->price = $_POST[$key];
						$hotelRoomAndDates->save();
						
						$returnArr[$key] = $_POST[$key];
					} else {
						$hotelRoomAndDates = new HotelRoomAndDates();
						$hotelRoomAndDates->hotelRoomId = $hotelRoom->id;
						$hotelRoomAndDates->hotelDateGroupId = $col->id;
						$hotelRoomAndDates->price = null;
						$hotelRoomAndDates->save();
						
						$returnArr[$key] = null;
					}
				}
				
				echo '{';
				echo '"success":true,';
				echo '"msg":' . json_encode((object)$returnArr);
				echo '}';
				die();
			}
		}
		
		echo '{';
		echo '"success":false,';
		echo '"msg":""';
		echo '}';
		die();
	}
	
	public function actionUpdatedata()
	{
		$hotelId = intval($_GET['hotelId']);
		
		if (strval($_POST['data']) != '') {
			$data = json_decode($_POST['data']);
			
			if (is_array($data)) {
				foreach($data as $obj) {
					HotelRoomAndDates::updateData($obj, $hotelId);
				}
			} else if (is_object($data)) {
				HotelRoomAndDates::updateData($data, $hotelId);
			}
		}
		
		die();
	}
	
	public function actionDelete()
	{
		$roomId = intval($_GET['id']);
		
		HotelRoom::model()->deleteAll('id=:id', array(':id' => $roomId));
		HotelRoomAndDates::model()->deleteAll('hotelRoomId=:hotelRoomId', array(':hotelRoomId' => $roomId));
		
		die();
	}
	
	public function actionDie()
	{
		die();
	}
}